package org.cfx.auth.controller;

import org.cfx.auth.domain.dto.RoleDto;
import org.cfx.auth.service.IAuthPermissionService;
import org.cfx.auth.domain.AuthPermission;
import org.cfx.auth.query.AuthPermissionQuery;
import org.cfx.basic.annotation.Permission;
import org.cfx.basic.util.AjaxResult;
import org.cfx.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RTX 9090
 */
@RestController
@RequestMapping("/authPermission")
@Permission(name = "权限管理")
public class AuthPermissionController {
    @Autowired
    public IAuthPermissionService authPermissionService;


    /**
     * 保存和修改公用的
     * @param authPermission  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @Permission(name = "权限的添加或修改")
    public AjaxResult addOrUpdate(@RequestBody AuthPermission authPermission){
        try {
            if( authPermission.getId()!=null) {
                authPermissionService.update(authPermission);
            } else {
                authPermissionService.add(authPermission);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @Permission(name = "根据id删除权限")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            authPermissionService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    @DeleteMapping(value="/deleteList")
    @Permission(name = "权限批量删除")
    public AjaxResult deleteList(List<Long> ids){
        try {
            authPermissionService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            AuthPermission authPermission = authPermissionService.queryById(id);
            return AjaxResult.me().setData(authPermission);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< AuthPermission> list = authPermissionService.queryAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody AuthPermissionQuery query)
    {
        try {
            PageList<AuthPermission> pageList = authPermissionService.queryAllPage(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    @GetMapping("/permissionTree")
    public AjaxResult getRoleTree(){
        List<AuthPermission> authPermissions = authPermissionService.queryTree();
        return AjaxResult.me().setData(authPermissions);
    }

}
